﻿using AutoMapper;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;

namespace MyMES.Read.API.Application.Handler.Inventorylistss
{
    public class GetInventorylistHandler : IRequestHandler<GetInventorylistCommand, APIResult<List<Inventorylist>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Inventorylist> resss;

        /// <summary>
        /// 库存清单
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="resss"></param>
        public GetInventorylistHandler(IMapper mapper,IBaseRepository<Inventorylist> resss)
        {
            this.mapper = mapper;
            this.resss = resss;
        }
        /// <summary>
        /// 库存
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<List<Inventorylist>>> Handle(GetInventorylistCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<Inventorylist>> result = new APIResult<List<Inventorylist>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "显示成功";
                string sql = "select *  from Inventorylist";
                var list = await resss.SelectAsync(sql);
                if (!string.IsNullOrEmpty(request.WarehouseName))
                {
                    list = list.Where(x => x.WarehouseName.Contains(request.WarehouseName)).ToList();
                }

                if (!string.IsNullOrEmpty(request.InventoryNum))
                {
                    list = list.Where(x => x.InventoryNum.Contains(request.InventoryNum)).ToList();
                }
                if (!string.IsNullOrEmpty(request.InventoryNumber))
                {
                    list = list.Where(x => x.InventoryNumber.Contains(request.InventoryNumber)).ToList();
                }
                result.data = list;
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        
        }
    }
}
